<template xmlns="http://www.w3.org/1999/html">
  <div class="outer">
    <div style="">
      <el-card class="box-card" style="filter: opacity(95%)">
        <h1>智慧社区登录</h1>
        <el-form ref="user" :model="user" label-width="100px" :rules="rules" class="demo-ruleForm">
          <el-row>
            <el-col :span="20">
              <el-form-item label="账号" prop="username">
                <el-input v-model="user.username"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="密码" prop="password" label-width="100px">
                <el-input type="password" v-model="user.password"></el-input>
                <span style="color: red">{{ message }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item prop="code" label="验证码">
                <el-input
                    v-model="user.code"
                    type="text"
                    auto-complete="off"
                    placeholder="验证码"
                    :prefix-icon="getUrl('confirm')"
                    prefix-icon="el-icon-s-check"
                >
                </el-input>
              </el-form-item>
            </el-col>
              <el-col :span="20">
                <el-form-item>
                <div class="login-code">
                  <img
                      class="login-code-img"
                      :src="codeUrl"
                      alt="验证码"
                      @click="getCode"
                  />
                  <svg-icon
                      class="refresh-icon"
                      icon-class="refresh"
                      @click="getCode"
                  />
                </div>
                </el-form-item>
              </el-col>


          </el-row>


          <el-button type="primary" @click="submitForm('user')">登录</el-button>

        </el-form>
      </el-card>
    </div>

  </div>
</template>

<script>

export default {
  name: "LoginView",
  data() {
    return {
      codeUrl: "",//图片路径
      message: "", //错误信息
      user: { //账号密码
        username: "",
        password: "",
        code: "",

      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 5 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
          {min: 4, max: 4, message: '长度4个字符', trigger: 'blur'}
        ],


      }
    }
  },
  methods: {
    // 获取验证码
    getCode() {
      // 调用获取验证码图片的接口，将返回的URL赋值给codeUrl
      this.axios.get("getCode")
          .then(result => {
            this.codeUrl = result.data.image;
            this.code = result.data.code;
            console.log(result)
          });
    },
    //登录请求
    login() {
      console.log(this.user);
      loading: true;


      //发送请求
      if (this.code === this.user.code) {
        this.axios.post("login", "username=" + this.user.username +
            "&password=" + this.user.password)
            .then(result => {
              console.log(result.data);
              // 判断账号状态
              if (result.data.code == 403) {
                this.$message({
                  type: 'error',
                  message: '该账号已被禁用！'
                })
                return
              }
              //将token保存到localStorage
              if (result.data.code == 200) {
                localStorage.setItem("token", result.data.data);
                localStorage.setItem("username", this.user.username)

                //跳转主页面
                this.$router.push({path: '/main'})
              } else {
                this.message = result.data.message
                // 获取登录失败时的用户名
                const failedUsername = this.user.username;

                // 发送请求到后端，传递登录失败的用户名
                // this.axios.post("user", {username: failedUsername})
                // this.$message({
                //     type: 'error',
                //     message: '账号或密码错误！'
                // })
              }
            });
      } else {
        this.$message({
          type: 'error',
          message: '验证码错误！'
        })
        this.getCode();
      }

    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(valid)
          this.login()
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 获取本地图片地址
    getUrl(value) {
      return require(`@/assets/icons/svg/${value}.svg`)
    },


  },
  mounted() {
    this.getCode()
  }
}
</script>

<style scoped>
@import "@/assets/styles/reset.css";

.box-card {
  margin: 0px auto;
  width: 400px;
}

body {
  margin-top: 0;
  margin-left: 0;
}

.outer {
  height: 100%;
  width: 100%;

  background-image: url("/src/assets/backgroundImg/Snipaste_2023-07-13_08-48-13.jpg");
  background-repeat: repeat-x;
  background-size: cover;
  background-size: 100% 100%;
}

.login-code {
  width: 30%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-code-img {
  height: 60px;
}

h1 {
  margin-bottom: 30px;
}

/deep/ .el-card {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.confirm{
  /*no-repeat:图片不重复；10px：控制图片左边距 center：图片上下居中*/
  /*background:url(../assets/icons/svg/confirm.svg) no-repeat 10px center;*/
  /*padding: 0px 50px; !*输入内容左右边距控制*!*/
  /*width: 534px;*/
  //height: 66px;
  //font-size: 23px;
  //color: #dadada;
}

</style>
